package com.kurumi.util;

import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.collections.MapUtils;

public class CSVUtil {
	
	 /**
     * 处理下载指标
     *
     */
    public static String handleExportData(Map<String,Object> reportData, String[] columns){
        StringBuilder sb = new StringBuilder();
        for (String columnName:columns) {
           //addStringBuffer(sb,reportData,columnName);
        	String value =  null;
			/*
			 * if(columnName.startsWith("C06x") || columnName.startsWith("C07x") ||
			 * columnName.startsWith("C08x") || columnName.startsWith("C14x") ||
			 * columnName.startsWith("C15x") || columnName.startsWith("C16x") ||
			 * columnName.startsWith("C17x") || columnName.startsWith("C18x") ||
			 * columnName.startsWith("C21x") || columnName.startsWith("C22x") ||
			 * columnName.startsWith("C23x") || columnName.startsWith("C19x") ||
			 * columnName.startsWith("C20x") || columnName.startsWith("C35x") ||
			 * columnName.startsWith("C36x") || columnName.startsWith("C37x") ||
			 * columnName.startsWith("C38x") || columnName.startsWith("C39x") ||
			 * columnName.startsWith("C42x") || columnName.startsWith("C43x") ||
			 * columnName.startsWith("C44x") || columnName.startsWith("C40x") ||
			 * columnName.startsWith("C41x") || columnName.startsWith("A18x") ||
			 * columnName.startsWith("D19x") || columnName.startsWith("D20x") ||
			 * columnName.startsWith("D23x")) { value = MapUtils.getString(reportData,
			 * columnName); }else {
			 */
        	//value = MapUtils.getString(reportData, columnName.toUpperCase());
        	//}
        	
        	value = MapUtils.getString(reportData, columnName);
        	
        	 if ("null".equals(value) || value == null) {
        		 value = "";
			} 
        	 //去除
        	 value = value.replaceAll(",", "，");
        	 value = replaceBlank(value);
        	 
        
        	
        	sb.append("\t").append(value).append(",");
        	
        	
        	
        	
        }
        sb.append("\n");
        return  sb.toString();
    }
    
    
	/**
	 * 去除字符串中的回车、换行符、制表符
	 * @param str
	 * @return
	 */
	 public static String replaceBlank(String str) {  
	        String dest = "";  
	        if (str!=null) {  
	            Pattern p = Pattern.compile("\\t|\r|\n");  
	            Matcher m = p.matcher(str);  
	            dest = m.replaceAll("");  
	        }  
	        return dest;  
	    }  
  
 
    
    
    public static void addStringBuffer(StringBuilder sb, Map<String, Object> map,String name){
        if(map.get(name) == null ){
            sb.append("-,");
        }else{
            String value = String.valueOf(map.get(name));
            String temp = value.replaceAll("\r", "").replaceAll("\n", "");
            if(temp.contains(",")){
                if(temp.contains("\"")){
                    temp=temp.replace("\"", "\"\"");
                }
                //将逗号转义
                temp="\""+temp+"\"";
            }
            sb.append("\t").append(temp).append(",");
        }
    }
 
}

